package feifei.homeworkfei;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet("/queryMessage.action")
public class QueryMessageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String referer = req.getHeader("referer");
        // URL解码
        String decode = URLDecoder.decode(referer,"UTF-8");
        // 判断localhost:8080 如果是的话，代表是从自己网站发起的请求，就把b.html页面返回给客户端
        // 如果不是的话，跳转到自己网站的index.html首页，从index.html访问b.html页面信息
        // www.xiaobaotu.com 是假网站的域名
        // localhost是真网站的域名
        if (decode.contains("localhost")) {
            // 正常访问
            resp.sendRedirect( req.getContextPath() + "/b.html");
        } else {
            // 跳转到index.html
            resp.sendRedirect(req.getContextPath() + "/index.html");
        }
    }

}
